from jinja2 import Environment, FileSystemLoader
import datetime

from pathlib import Path
import sys
import os
curPath = os.path.abspath(os.path.dirname(__file__))
rootPath = os.path.split(curPath)[0]
sys.path.append(rootPath)

html_template_path = os.path.join(rootPath,"stock_visual/temp/template.html")

#报表自动生成（这里使用html生成报表，扩展性比pdf强）
#基于mpfinance绘制的K线图对模板进行动态渲染
class Stock_mpf_Render_Json:
    def __init__(self):
        #动态渲染的字段如下
        self.stock_render_json = {
            'code': '',
            'codeName': '',
            'minute_kline_path': '',
            'daily_kline_path': '',
            'week_kline_path': '',
            "month_kline_path": ''
        }
    def set_stock_render_json(self,code,codeName,minute_kline_path="",daily_kline_path="",week_kline_path="",month_kline_path=""):
        self.stock_render_json['code'] = code
        self.stock_render_json['codeName'] = codeName
        self.stock_render_json['minute_kline_path'] = minute_kline_path
        self.stock_render_json['daily_kline_path'] = daily_kline_path
        self.stock_render_json['week_kline_path'] = week_kline_path
        self.stock_render_json['month_kline_path'] = month_kline_path

    def get_stock_render_json(self):
        return self.stock_render_json

#基于mpfinance自动生成html报表
class Report_Based_Mpf:

    def __init__(self,stock_json_list):
        '''
        @param stock_json_list 多只股票动态渲染的json列表,type=list
        '''
        self.stock_json_list = stock_json_list

    def generate_html(self, html_savePath):
        '''
        @param html_savePath  html保存到路径；type=str
        '''
        template_dir,template_path = str(Path(html_template_path).parent),str(Path(html_template_path).name)
        today = datetime.datetime.now().strftime("%Y-%m-%d")
        env = Environment(loader=FileSystemLoader(template_dir))
        template = env.get_template(template_path)
        with open(html_savePath, 'w+') as fout:
            html_content = template.render(today=today,
                                           stocks=self.stock_json_list)
            fout.write(html_content)

#基于pyechart自动生成html报表
class Report_Based_Pyechart:
    pass